package me.shurufa.activities;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.util.Util;
import com.github.ybq.android.spinkit.SpinKitView;
import com.malinskiy.superrecyclerview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import cn.finalteam.okhttpfinal.HttpRequest;
import de.greenrobot.event.EventBus;
import me.shurufa.R;
import me.shurufa.adapter.CommentListAdapter;
import me.shurufa.bean.BookComment;
import me.shurufa.bean.CommentEntity;
import me.shurufa.bean.Excerpt;
import me.shurufa.bean.UserInfoEntity;
import me.shurufa.db.PersistenceUtils;
import me.shurufa.net.API;
import me.shurufa.net.RequestParams;
import me.shurufa.net.okhttp.callback.HttpCallback;
import me.shurufa.response.BookCommentCommentResp;
import me.shurufa.response.ExcerptCommentResp;
import me.shurufa.response.SendCommentResp;
import me.shurufa.utils.Constants;
import me.shurufa.utils.LogUtils;
import me.shurufa.utils.NetworkUtils;
import me.shurufa.utils.RecyclerViewStateUtils;
import me.shurufa.utils.Utils;
import me.shurufa.widget.LoadingFooter;
import me.shurufa.widget.commentbar.SimpleOnSendButtonClickListener;
import me.shurufa.widget.commentbar.SimpleSendCommentBar;
import me.shurufa.widget.recyclerview.EndlessRecyclerOnScrollListener;
import me.shurufa.widget.recyclerview.HeaderAndFooterRecyclerViewAdapter;

/**
 * Create by andy (https://github.com/andyxialm)
 * Create time: 16/5/17 09:25
 * Description :
 */
public class CommentDetailActivity extends BaseActivity implements View.OnClickListener, CommentListAdapter.OnCommentClickListener {

    @Bind(R.id.recycler_view)
    SuperRecyclerView mRecyclerView;

    @Bind(R.id.right_text)
    TextView right_text;

    @Bind(R.id.progress)
    SpinKitView progress;

    @Bind(R.id.scb_comment_bar)
    SimpleSendCommentBar scb_comment_bar;

    private View headerView;
    private List<CommentEntity> mDataList;
    private CommentListAdapter mAdapter;
    private HeaderAndFooterRecyclerViewAdapter mHeaderAndFooterRecyclerViewAdapter;

    private String mBookName;
    private long id;
    private int mType;
    private boolean isReply;

    // 键盘弹出需要
    private int mBaseBottomHeight; // 用于弹出键盘列表位移
    private int mKeyboardSize;
    private boolean mIsOpenKeyborad;
    private boolean mIsLastPos; // 点击的是否是最后一个

    private EndlessRecyclerOnScrollListener mOnScrollListener = new EndlessRecyclerOnScrollListener() {
        @Override
        public void onLoadNextPage(View view) {
            super.onLoadNextPage(view);

            LoadingFooter.State state = RecyclerViewStateUtils.getFooterViewState(mRecyclerView.getRecyclerView());
            if(state == LoadingFooter.State.Loading) {
                LogUtils.kLog().e("the state is Loading, just wait..");
                return;
            }

            if (hasMore) {
                if (!NetworkUtils.isNetAvailable(CommentDetailActivity.this)) {
                    RecyclerViewStateUtils.setFooterViewState(CommentDetailActivity.this, mRecyclerView.getRecyclerView(), Constants.PAGE_REQUEST_COUNT, LoadingFooter.State.NetWorkError, mFooterClick);
                    return;
                }
                // loading more
                RecyclerViewStateUtils.setFooterViewState(CommentDetailActivity.this, mRecyclerView.getRecyclerView(), Constants.PAGE_REQUEST_COUNT, LoadingFooter.State.Loading, null);
                loadData();
            } else {
                //the end
                RecyclerViewStateUtils.setFooterViewState(CommentDetailActivity.this, mRecyclerView.getRecyclerView(), Constants.PAGE_REQUEST_COUNT, LoadingFooter.State.TheEnd, null);
            }
        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            /*if (scb_comment_bar != null && scb_comment_bar.getVisibility() == View.VISIBLE) {
                scb_comment_bar.setVisibility(View.GONE);*/

        }
    };

    // 刷新需要
    private int mPage = 1;
    private boolean hasMore = true;
    private View.OnClickListener mFooterClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (NetworkUtils.isNetAvailable(CommentDetailActivity.this)) {
                RecyclerViewStateUtils.setFooterViewState(CommentDetailActivity.this, mRecyclerView.getRecyclerView(), Constants.PAGE_REQUEST_COUNT, LoadingFooter.State.Loading, null);
                loadData();
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        activity_layout_res = R.layout.activity_comment_detail;
        super.onCreate(savedInstanceState);
        parseArguments();
        initViews();
        initListener();
        loadData();
    }

    private void initListener() {
        getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                int screenHeight = getWindow().getDecorView().getRootView().getHeight();
                int heightDifference = screenHeight - (r.bottom - r.top);

                heightDifference -= r.top;
                // 过滤已经展开
                if (!mIsOpenKeyborad) {
                    return;
                }

                if (heightDifference >= mKeyboardSize) {
                    mKeyboardSize = heightDifference;
                    // 获得屏幕高度
                    WindowManager wm = getWindowManager();
                    int screenHeightReal = wm.getDefaultDisplay().getHeight();

                    // 获得点击控件的所在屏幕的高度(底部为准) mBaseBottomHeight
                    // 计算 1. 获得键盘顶部 和 item底部的偏移量
                    int keyboardTopHeightInParent = screenHeightReal - heightDifference;
                    int offset = - (keyboardTopHeightInParent - scb_comment_bar.getHeight() - mBaseBottomHeight);

                    // 如果点击是最后一个并且差值是负值(即还需要向下面滚，则自动过滤)
                    if (mIsLastPos && offset < 0) {
                        return;
                    }

                    mRecyclerView.getRecyclerView().scrollBy(0, offset);
                    //recyclerView.getRecyclerView().smoothScrollBy(0, offset);
                }
            }
        });

        /**
         * 触摸关闭评论条
         */
        mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //隐藏键盘
                if (event.getAction() == MotionEvent.ACTION_DOWN && scb_comment_bar.getVisibility() == View.VISIBLE) {
                    mIsOpenKeyborad = false;
                    Utils.closeInputMethod(CommentDetailActivity.this, scb_comment_bar.getEditText());
                    scb_comment_bar.getEditText().clearFocus();
                    scb_comment_bar.setVisibility(View.GONE);
                }
                return false;
            }
        });

        scb_comment_bar.setOnSendButtonClickListener(new SimpleOnSendButtonClickListener() {
            @Override
            public void onSend(String commentId, String content) {

                // 书摘 则是创建书摘评论  书评 则是回复评论
                if (mType == Constants.TYPE_COMMENT_EXCERPT && !isReply) {
                    reqCreateComment(commentId, content);
                } else {
                    reqReplyComment(commentId, content);
                }
            }
        });

        mAdapter.setOnCommentClickListener(this);
    }

    /**
     * 创建书摘的评论
     * @param commentId commentId
     * @param content   内容
     */
    private void reqCreateComment(final String commentId, final String content) {
        String type = "excerpt";
        RequestParams params = new RequestParams();
        params.addFormDataPart("id", commentId);
        params.addFormDataPart("type", type);
        params.addFormDataPart("content", content);

        HttpRequest.post(API.CREATE_COMMENT, params, new HttpCallback<SendCommentResp>(this) {
            @Override
            public void onRespSuccess(SendCommentResp resp) {
                try {
                    CommentEntity commentEntity = new CommentEntity();
                    commentEntity.id = resp.id;
                    commentEntity.content = content;
                    commentEntity.created_at = System.currentTimeMillis() / 1000;

                    UserInfoEntity userInfoEntity = new UserInfoEntity();
                    userInfoEntity.avatar = PersistenceUtils.getAvatar();
                    userInfoEntity.username = PersistenceUtils.getUsername();
                    userInfoEntity.id = Integer.parseInt(PersistenceUtils.getUserId());
                    userInfoEntity.user_id = Integer.parseInt(PersistenceUtils.getUserId());
                    commentEntity.user_info = userInfoEntity;

                    mDataList.add(0, commentEntity);
                    mAdapter.notifyItemInserted(0);

                    Utils.showToast("评论成功");
                    // 清空EditText
                    if (scb_comment_bar != null && scb_comment_bar.getEditText() != null) {
                        scb_comment_bar.getEditText().setText(null);
                    }

                    // 发送Event
                    Intent in = new Intent(Constants.EVENT_SEND_COMMENT);
                    in.putExtra(Constants.ARG_PARENT_ID, (int) id);
                    in.putExtra(Constants.ARG_PARENT_TYPE, Constants.TYPE_COMMENT_EXCERPT);
                    EventBus.getDefault().post(in);

                } catch (Exception e) {
                }

            }

            @Override
            public void onRespFailure(SendCommentResp resp) {
                scb_comment_bar.setVisibility(View.VISIBLE);
                Utils.showSoftkeyboard(CommentDetailActivity.this, scb_comment_bar.getEditText());
                Utils.showToast(R.string.sys_err);
            }

            @Override
            public void onStart() {
                super.onStart();
                scb_comment_bar.setVisibility(View.GONE);
                Utils.closeInputMethod(CommentDetailActivity.this, scb_comment_bar.getEditText());
            }

            @Override
            public void onFinish() {
                super.onFinish();
                scb_comment_bar.getEditText().setText(null);
                scb_comment_bar.setVisibility(View.GONE);
            }
        });
    }

    /**
     * 创建评论 回复评论
     * @param commentId 评论id
     * @param content   内容
     */
    private void reqReplyComment(final String commentId, final String content) {
        RequestParams params = new RequestParams();
        params.addFormDataPart("id", commentId);
        params.addFormDataPart("content", content);

        HttpRequest.post(API.REPLY_COMMENT, params, new HttpCallback<SendCommentResp>(this) {
            @Override
            public void onRespSuccess(SendCommentResp resp) {
                try {
                    CommentEntity commentEntity = new CommentEntity();
                    commentEntity.id = resp.id;
                    commentEntity.content = content;
                    commentEntity.created_at = System.currentTimeMillis() / 1000;

                    UserInfoEntity userInfoEntity = new UserInfoEntity();
                    userInfoEntity.avatar = PersistenceUtils.getAvatar();
                    userInfoEntity.username = PersistenceUtils.getUsername();
                    userInfoEntity.id = Integer.parseInt(PersistenceUtils.getUserId());
                    userInfoEntity.user_id = Integer.parseInt(PersistenceUtils.getUserId());
                    commentEntity.user_info = userInfoEntity;

                    mDataList.add(0, commentEntity);
                    mAdapter.notifyDataSetChanged();

                    // 清空EditText
                    if (scb_comment_bar != null && scb_comment_bar.getEditText() != null) {
                        scb_comment_bar.getEditText().setText(null);
                    }

                    // 发送Event
                    Intent in = new Intent(Constants.EVENT_SEND_COMMENT);
                    in.putExtra(Constants.ARG_PARENT_ID, (int) id);
                    in.putExtra(Constants.ARG_PARENT_TYPE, mType == Constants.TYPE_COMMENT_EXCERPT ? Constants.TYPE_COMMENT_EXCERPT : Constants.TYPE_COMMENT_BOOK_COMMENT);
                    EventBus.getDefault().post(in);
                } catch (Exception e) {
                }
            }

            @Override
            public void onRespFailure(SendCommentResp resp) {
                scb_comment_bar.setVisibility(View.VISIBLE);
                Utils.showToast(R.string.sys_err);
            }

            @Override
            public void onStart() {
                super.onStart();
                scb_comment_bar.setVisibility(View.GONE);
                Utils.closeInputMethod(CommentDetailActivity.this, scb_comment_bar.getEditText());
            }
        });
    }


    /**
     * 初始化
     *
     * @param in       Intent
     * @param bookName 书名
     * @param type     内容类型 书摘还是书评
     */
    public static void initArguments(Intent in, String bookName, long id, int type) {
        in.putExtra(Constants.ARG_ID, id);
        in.putExtra(Constants.ARG_BOOK_TITLE, bookName);
        in.putExtra(Constants.ARG_COMMENT_CONTENT, type);
    }


    private void parseArguments() {
        mBookName = getIntent().getStringExtra(Constants.ARG_BOOK_TITLE);
        mType = getIntent().getIntExtra(Constants.ARG_COMMENT_CONTENT, 0);
        id = getIntent().getLongExtra(Constants.ARG_ID, 0l);
    }

    private void initViews() {
        titleTextView.setText(mBookName);
        right_text.setText(R.string.comment);
        right_text.setVisibility(View.VISIBLE);
        right_text.setOnClickListener(this);

        mDataList = new ArrayList<>();
        mAdapter = new CommentListAdapter(this, mDataList);
        mHeaderAndFooterRecyclerViewAdapter = new HeaderAndFooterRecyclerViewAdapter(mAdapter);
        mHeaderAndFooterRecyclerViewAdapter.addHeaderView(getHeaderView());
        mHeaderAndFooterRecyclerViewAdapter.getHeaderView().setVisibility(View.GONE);
        mRecyclerView.setAdapter(mHeaderAndFooterRecyclerViewAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.getRecyclerView().setOverScrollMode(View.OVER_SCROLL_NEVER);
        mRecyclerView.setOnScrollListener(mOnScrollListener);
    }

    private View getHeaderView() {
        if (mType == Constants.TYPE_COMMENT_EXCERPT) {
            // 书摘
            headerView = View.inflate(this, R.layout.list_item_bookdetail_digest, null);
            // 隐藏social bar
            headerView.findViewById(R.id.social_bar_layout).setVisibility(View.GONE);
            return headerView;
        }



        else {
            // 书评
            headerView = View.inflate(this, R.layout.list_item_bookdetail_comment, null);
            // 隐藏social bar
            headerView.findViewById(R.id.layout_social_bar).setVisibility(View.GONE);
            return headerView;
        }
    }

    /**
     * 记载数据
     */
    private void loadData() {
        try {
            if (mType == Constants.TYPE_COMMENT_EXCERPT) {
                loadDataExcerpt(id, mPage);
                return;
            }

            if (mType == Constants.TYPE_COMMENT_BOOK_COMMENT) {
                loadDataBookComment(id, mPage);
            }
        } catch (Exception e) {
            Utils.showToast(R.string.sys_err);
        }
    }

    /**
     * 加载书评和评论数据
     */
    private void loadDataBookComment(long bookCommentId, final int page) {
        RequestParams params = new RequestParams();
        params.addFormDataPart("id", bookCommentId);
        params.addFormDataPart("page", page);

        HttpRequest.post(API.GET_BOOKCOMMENT_COMMENTS, params, new HttpCallback<BookCommentCommentResp>() {

            @Override
            public void onRespSuccess(final BookCommentCommentResp resp) {
                // 设置headerView
                if (page == 0 || page == 1) {
                    setHeaderViewBookComment(resp.data);
                }
                handleResponse(resp.comments, (page == 0 || page == 1));
            }

            @Override
            public void onRespFailure(BookCommentCommentResp resp) {

            }

            @Override
            public void onStart() {
                super.onStart();
                if (NetworkUtils.isNetAvailable(CommentDetailActivity.this)) {
                    RecyclerViewStateUtils.setFooterViewState(mRecyclerView.getRecyclerView(), LoadingFooter.State.Loading);

                    if (page <= 1) {
                        progress.setVisibility(View.VISIBLE);
                    }

                } else {
                    // 是上拉加载
                    if (mPage > 1) {
                        RecyclerViewStateUtils.setFooterViewState(CommentDetailActivity.this, mRecyclerView.getRecyclerView(), Constants.PAGE_REQUEST_COUNT, LoadingFooter.State.NetWorkError, mFooterClick);
                    }
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();
                refreshComplete();
                if (page <= 2) {
                    progress.setVisibility(View.GONE);
                }
            }
        });
    }

    /**
     * 设置HeaderView的内容
     * @param bookComment
     */
    public void setHeaderViewBookComment(BookComment bookComment) {
        mHeaderAndFooterRecyclerViewAdapter.getHeaderView().setVisibility(View.VISIBLE);
        // 书评
        TextView tv_comment_content = (TextView) headerView.findViewById(R.id.tv_comment_content);
        TextView tv_info_user = (TextView) headerView.findViewById(R.id.tv_info_user);
        TextView tv_info_time = (TextView) headerView.findViewById(R.id.tv_info_time);

        tv_comment_content.setText(bookComment.content);
        tv_info_user.setText(bookComment.user_info.username);
        tv_info_time.setText(Utils.getFormatTime(bookComment.created_at));

        // 隐藏social bar
        headerView.findViewById(R.id.layout_social_bar).setVisibility(View.GONE);
    }

    /**
     * 设置HeaderView书摘的内容
     * @param excerpt 书摘
     */
    public void setHeaderViewExcerpt(Excerpt excerpt) {
        mHeaderAndFooterRecyclerViewAdapter.getHeaderView().setVisibility(View.VISIBLE);
        ImageView iv_digest_content = (ImageView) headerView.findViewById(R.id.iv_digest_content);
        TextView tv_digest_content = (TextView) headerView.findViewById(R.id.tv_digest_content);
        TextView tv_mark_user = (TextView) headerView.findViewById(R.id.tv_mark_user);

        TextView tv_chapter = (TextView) headerView.findViewById(R.id.tv_chapter);
        final TextView tv_mark_content = (TextView) headerView.findViewById(R.id.tv_mark_content);
        final View layout_mark_content = headerView.findViewById(R.id.layout_mark_content);
        TextView tv_note_create_time = (TextView) headerView.findViewById(R.id.tv_note_create_time);

        TextView tv_no_note = (TextView) headerView.findViewById(R.id.tv_no_note);
        TextView tv_page_no = (TextView) headerView.findViewById(R.id.tv_page_no);
        final ImageView expand_view = (ImageView) headerView.findViewById(R.id.expand_view);

        if (Constants.DIGEST_TYPE_TEXT == excerpt.type) {
            iv_digest_content.setVisibility(View.GONE);
            tv_digest_content.setVisibility(View.VISIBLE);
            tv_digest_content.setText(excerpt.content);
        } else {
            iv_digest_content.setVisibility(View.VISIBLE);
            tv_digest_content.setVisibility(View.GONE);
            // 图片书摘
            Glide.with(this)
                    .load(excerpt.screenshot + Constants.THUMB_SUFFIX)
                    .centerCrop()
                    .placeholder(R.drawable.ic_placeholder_digest_image)
                    .error(R.drawable.ic_placeholder_digest_image)
                    .into(iv_digest_content);
        }

        // 用户
        UserInfoEntity user_info = excerpt.user_info;
        if (null != user_info) {
            tv_mark_user.setText(user_info.username);
        }

        // 笔记
        tv_chapter.setText(getString(R.string.chapter_suffix) + excerpt.menu_title);
        tv_chapter.setVisibility(TextUtils.isEmpty(excerpt.menu_title) ? View.INVISIBLE : View.VISIBLE);
        tv_mark_content.setText(excerpt.note);
        tv_note_create_time.setText(Utils.getFormatTime(String.valueOf(excerpt.updated_at)));

        // 若无笔记内容，则显示暂无笔记
        boolean hasNote = !TextUtils.isEmpty(excerpt.note);
        tv_mark_content.setVisibility(hasNote ? View.VISIBLE : View.GONE);
        tv_no_note.setVisibility(hasNote ? View.GONE : View.VISIBLE);

        // 页数
        tv_page_no.setText("P."+excerpt.page_num);
        tv_page_no.setVisibility(excerpt.page_num == 0 ? View.INVISIBLE : View.VISIBLE);

        expand_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layout_mark_content.setVisibility(layout_mark_content.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
                expand_view.setImageResource((layout_mark_content.getVisibility() == View.VISIBLE) ? R.drawable.ic_arrow_top : R.drawable.ic_arrow_bottom);
            }
        });

        // 隐藏social bar
        headerView.findViewById(R.id.social_bar_layout).setVisibility(View.GONE);
    }

    /**
     * 加载书摘和评论数据
     * @param excerptId
     */
    private void loadDataExcerpt(long excerptId, final int page) {
        RequestParams params = new RequestParams();
        params.addFormDataPart("id", excerptId);
        params.addFormDataPart("page", page);

        HttpRequest.post(API.GET_EXCERPT_COMMENTS, params, new HttpCallback<ExcerptCommentResp>() {

            @Override
            public void onRespSuccess(final ExcerptCommentResp resp) {
                try {
                    // 设置headerView
                    if (page == 0 || page == 1) {
                        setHeaderViewExcerpt(resp.data);
                    }
                    handleResponse(resp.comments, (page == 0 || page == 1));
                } catch (Exception e){
                }
            }

            @Override
            public void onRespFailure(ExcerptCommentResp resp) {

            }

            @Override
            public void onStart() {
                super.onStart();
                if (NetworkUtils.isNetAvailable(CommentDetailActivity.this)) {
                    RecyclerViewStateUtils.setFooterViewState(mRecyclerView.getRecyclerView(), LoadingFooter.State.Loading);

                    if (page <= 1) {
                        progress.setVisibility(View.VISIBLE);
                    }

                } else {
                    // 是上拉加载
                    if (mPage > 1) {
                        RecyclerViewStateUtils.setFooterViewState(CommentDetailActivity.this, mRecyclerView.getRecyclerView(), Constants.PAGE_REQUEST_COUNT, LoadingFooter.State.NetWorkError, mFooterClick);
                    }
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();
                refreshComplete();
                if (page <= 2) {
                    progress.setVisibility(View.GONE);
                }
            }
        });
    }

    /**
     * 处理返回结果
     * 1. 去重
     * @param commentList     commentList
     * @param isPullToRefresh  boolean
     */
    private void handleResponse(List<CommentEntity> commentList, boolean isPullToRefresh) {
        // 是否还有更多
        hasMore = Utils.isNullForList(commentList) ? false : commentList.size() < Constants.PAGE_REQUEST_COUNT ? false : true;

        // 1. 添加进列表并刷新
        mAdapter.append(commentList);

        // 2. 页码自增
        mPage ++;

    }

    /**
     * Require run in UIThread
     */
    public void refreshComplete() {
        // 是上拉加载更多
        if (mPage > 1) {
            RecyclerViewStateUtils.setFooterViewState(mRecyclerView.getRecyclerView(), hasMore ? LoadingFooter.State.Normal : LoadingFooter.State.TheEnd);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.right_text:
                if (!PersistenceUtils.isLogin()) {
                    Utils.showToast(R.string.please_login);
                    return;
                }

                scb_comment_bar.setCommentId(String.valueOf(id));
                scb_comment_bar.setVisibility(View.VISIBLE);
                scb_comment_bar.getEditText().requestFocus();
                Utils.showSoftkeyboard(CommentDetailActivity.this, scb_comment_bar.getEditText());
                isReply = false;
                break;
        }
    }

    /**
     * 点击Item 回复评论
     * @param v          view
     * @param position   列表中的位置
     * @param commentEntity Bean
     */
    @Override
    public void onClick(View v, int position, CommentEntity commentEntity) {
        scb_comment_bar.setCommentId(String.valueOf(commentEntity.id));
        scb_comment_bar.setVisibility(View.VISIBLE);
        scb_comment_bar.requestFocus();

        int location[] = new int[2];
        v.getLocationOnScreen(location);
        mBaseBottomHeight = location[1] + v.getHeight();
        mIsOpenKeyborad = true;
        mIsLastPos = (mAdapter.getItemCount() == position + 1);
        Utils.showSoftkeyboard(CommentDetailActivity.this, scb_comment_bar.getEditText());
        isReply = true;
    }

    @Override
    public void onBackPressed() {
        if (scb_comment_bar != null && scb_comment_bar.getVisibility() == View.VISIBLE ) {
            scb_comment_bar.setVisibility(View.GONE);
            scb_comment_bar.getEditText().clearFocus();
        } else {
            super.onBackPressed();
        }
    }
}
